<?php
/**
 * Database.php
 *
 * @category    Madoqua
 * @package     Validate
 */

/** base class */
require_once 'Zend/Validate/Abstract.php';

/**
 * Madoqua_Validate_Database
 *
 * validates a database connection
 *
 * @category    Madoqua
 * @package     Validate
 */
class Madoqua_Validate_Database extends Zend_Validate_Abstract
{
    /**
     * adapter
     *
     * @var unknown_type
     */
    protected $_adapter;

    /**
     * epic
     */
    const FAIL = 1;

    /**
     * Validation failure message template definitions
     *
     * @var array
     */
    protected $_messageTemplates = array(
    self::FAIL    => "'%value%' does not produce a database connection, check your values and try again"
    );

    /**
     * constructor
     *
     * @param string $adapter
     */
    public function __construct($adapter)
    {
        $this->_adapter = $adapter;
    }

    /**
     * validate login
     *
     * @param string $value
     */
    public function isValid($value) {
        $values = func_get_arg(1);
        //values from the form
        
        $this->_setValue($values['username'] . '|' . $values['password'] . '@' . $values['host'] . '/' . $values['dbname']);
        //the entire value

        try {
            $db = Zend_Db::factory($this->_adapter, array(
                'host' => $values['host'],
                'username' => $values['username'],
                'password' => $values['password'],
                'dbname' => $values['dbname']
            ));
            //build a db obj.
            
            $con = $db->getConnection();
            //try to rertrieve the connection
        } catch (Exception $e) {
            //fail! :(
            $this->_error(self::FAIL);
            return false;
        }

        return true;
        
        
    }
}